﻿using Abp.AutoMapper;
using Abp.Threading;
using AutoMapper;
using MyCompanyName.AbpZeroTemplate.IRepositorys;
using System;
using System.Collections.Generic;
using System.Linq;
using MyCompanyName.AbpZeroTemplate.Product.Dto;

namespace MyCompanyName.AbpZeroTemplate.Product
{
    public class ProductService : AbpZeroTemplateAppServiceBase,IProductService
    {
        
         private readonly IProductRepository _productRepository;
        public  ProductService(IProductRepository productRepository)
        {
            this._productRepository = productRepository;
        }
        public int CreateProduct(CreateProductInputDto input)
        {
            var task = Mapper.Map<Product>(input);
            task.CreationTime = DateTime.Now;

           return   _productRepository.InsertAndGetId(task);
        }

        public ProductInputDto GetProductById(int productId)
        {
            return _productRepository.Get(productId).MapTo<ProductInputDto>();
        }

        public List<ProductInputDto> GetProductList(string productCode, string productName)
        {
            List<ProductInputDto> result = new List<ProductInputDto>();

            var obj = _productRepository.GetAll();
            result = obj.MapTo<List<ProductInputDto>>().Where(m => m.ProductCode.Contains(productCode)).ToList();

            return result;
        }

        public bool UpdatePorduct(UpdateProductDto input)
        {
            bool result = false;
            try
            {  //获取当前用户
                var currentUser = AsyncHelper.RunSync(this.GetCurrentUserAsync);
                var objUpdate = _productRepository.Get(input.Id);

                objUpdate.ProductName = input.ProductName;
                objUpdate.ProductType = input.ProductType;
                objUpdate.Remark = input.Remark;
                objUpdate.Effectivedate = input.Effectivedate;
                objUpdate.Invaliddate = input.Invaliddate;
                objUpdate.Priority = input.Priority;

                result = true;
            }
            catch (Exception ex)
            {
                result = false;
            }
            return result;
        }
    }
}
